<template>
    <div id="report">
        <div class="card-container">
            <el-row :gutter="10">
                <el-form :model="queryParams" label-width="100px" ref="queryForm" :inline="true">
                    <el-col :span="19" :offset="0">
                        <el-form-item label="项目名称" prop="projectId">
                            <el-select v-model="queryParams.projectId" clearable :style="'width:215px'"
                                placeholder="请选择所属项目">
                                <el-option v-for="item in projectList" :key="item.projectId" :label="item.shortName"
                                    :value="item.projectId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="设备序列号" prop="deviceCode">
                            <el-input v-model="queryParams.deviceCode" :style="'width:215px'" placeholder="请输入设备序列号"
                                clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item label="设备类型" prop="deviceType">
                            <el-select v-model="queryParams.deviceType" clearable :style="'width:215px'"
                                placeholder="请选择设备类型">
                                <el-option v-for="item in this.dict.type.sys_device_mode" :key="item.value"
                                    :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="运行日期" prop="monthTime">
                            <el-date-picker type="months" value-format="yyyy-MM" v-model="queryParams.monthTime"
                                popper-class="date_form" :picker-options="pickerOptions" placeholder="选择一个或多个月">
                            </el-date-picker>
                            <!--  :clearable="false" -->
                            <el-date-picker type="dates" format="dd" value-format="yyyy-MM-dd" v-model="queryParams.dayTime"
                                popper-class="day_form" :picker-options="pickerOptions" placeholder="选择一个或多个天">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5" :offset="0">
                        <el-form-item>
                            <el-button icon="el-icon-search" size="mini" @click="handleQuery">搜索
                            </el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
        </div>

        <div class="card-container">
            <el-table v-loading="loading" :data="tableList" :row-class-name="tableRowClassName">
                <el-table-column prop="date" align="center" show-overflow-tooltip label="运行日期" min-width="100" />
                <el-table-column prop="projectName" align="center" show-overflow-tooltip label="项目名称" min-width="90" />
                <el-table-column prop="code" align="center" show-overflow-tooltip label="设备序列号" min-width="125">
                </el-table-column>
                <el-table-column prop="deviceType" align="center" show-overflow-tooltip label="设备类型" min-width="80">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_device_mode" :value="scope.row.deviceType" />
                    </template>
                </el-table-column>
                <el-table-column prop="deviceModel" align="center" show-overflow-tooltip label="设备型号" min-width="80" />
                <el-table-column prop="address" align="center" show-overflow-tooltip label="安装位置" min-width="250" />
                <el-table-column prop="should" align="center" show-overflow-tooltip label="实际签到/应签到" min-width="85" />
                <el-table-column prop="runDuration" align="center" show-overflow-tooltip label="运行时长" min-width="100" />
                <el-table-column prop="runRate" align="center" show-overflow-tooltip label="运行率" min-width="80" />
                <el-table-column prop="timeDelay" align="center" show-overflow-tooltip label="服务平均延时(s) " min-width="80">
                    <template slot-scope="scope">
                        <span>{{ scope.row.timeDelay == null ? '-' : (scope.row.timeDelay / 1000).toFixed(5) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="exceptionRestartSum" align="center" show-overflow-tooltip label="异常重启数"
                    min-width="80">
                    <template slot-scope="scope">
                        <span style="cursor: pointer;color: #ff4444;" v-show="scope.row.exceptionRestartSum != null"
                            @click="handleDetail(scope.row)">{{ scope.row.exceptionRestartSum == null ? '-' :
                                scope.row.exceptionRestartSum }}</span>
                    </template></el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="cueerntPage" :limit.sync="pageSize"
                @pagination="getPage" />
        </div>
        <el-dialog title="异常重启详情" :visible.sync="dialogVisible" width="30%">
            <div class="card-container">
                <el-table :data="exceptionData" border>
                    <el-table-column label="次数" type="index" align="center" width="50">
                    </el-table-column>
                    <el-table-column prop="date" align="center" show-overflow-tooltip label="重启时间" min-width="100" />
                </el-table>
            </div>

        </el-dialog>
    </div>
</template>

<script>
import { getdayRunRate, getdayExceptionDetail, getmonthRunRate } from '@/api/runStatistics/index.js'
import { getProject } from "@/api/runningLog/runningLog.js";
import moment from 'moment';
export default {
    dicts: [
        "sys_device_mode"
    ],
    data() {
        return {
            dialogVisible: false,
            queryParams: { projectId: null, deviceCode: null, deviceType: null, monthTime: [], monthList: null, dayTime: [], dayList: null },
            projectList: [],
            runRateList: [],
            total: 0,
            tableList: [],
            cueerntPage: 1,
            pageSize: 10,
            loading: false,
            exceptionData: [],
            pickerOptions: {
                //禁用当前日期之前的日期
                disabledDate(time) {
                    //Date.now()是javascript中的内置函数，它返回自1970年1月1日00:00:00 UTC以来经过的毫秒数。
                    return time.getTime() > Date.now();
                },
            },
        }
    },
    created() {
        this.getConfig()
        let nowMonth = moment().startOf('month').format('YYYY-MM')
        this.queryParams.monthTime.push(nowMonth)
        this.queryParams.monthList = nowMonth
        this.getList()
    },
    methods: {
        async getConfig() {
            const res = await getProject();
            if (res.code == 200) {
                this.projectList = res.data;
            }
        },
        tableRowClassName({ row, rowIndex }) {
            // if (Number(row.magnitude) >= 6) {
            //     return 'warning-row';
            // }
            // return '';
        },
        async handleDetail(row) {
            const res = await getdayExceptionDetail({
                time: row.date,
                deviceCode: row.code
            });
            if (res.code == 200) {
                this.dialogVisible = true
                this.exceptionData = res.data.map(item => {
                    return { date: item }
                })
                console.log(res, 'accessTime')
            }
        },
        // 获取列表数据
        async getList() {
            this.loading = true
            let res
            if (this.queryParams.dayTime?.length == 0 || !this.queryParams.dayTime) {
                res = await getmonthRunRate(this.queryParams)
            } else {
                this.queryParams.dayList = this.queryParams.dayTime?.join()
                res = await getdayRunRate(this.queryParams)
            }
            this.loading = false
            this.runRateList = []
            if (res.code == 200) {
                for (let index = 0; index < res.data.length; index++) {
                    for (let num = 0; num < res.data[index].runRateVOList.length; num++) {
                        this.runRateList.push(Object.assign({}, res.data[index].deviceRunLogVO, res.data[index].runRateVOList[num]))
                    }
                }
                this.$nextTick(() => {
                    this.getPage()
                })
            }

        },
        // 分页操作
        getPage() {
            let _data = []
            _data = this.runRateList.slice((this.cueerntPage - 1) * this.pageSize, this.cueerntPage * this.pageSize)
            this.total = this.runRateList.length
            this.tableList = _data
        },

        handleQuery() {
            this.cueerntPage = 1;
            this.pageSize = 10
            this.queryParams.monthList = this.queryParams.monthTime?.join()
            this.queryParams.dayList = this.queryParams.dayTime?.join()
            if ((this.queryParams.monthTime && this.queryParams.monthTime?.length > 0) || (this.queryParams.dayTime && this.queryParams.dayTime?.length > 0)) {
                this.getList()
            } else {
                this.$modal.msgError('运行日期不能为空')
            }

        },
        resetQuery() {
            this.queryParams.monthList = moment().startOf('month').format('YYYY-MM')
            // 获取当前月份的天数
            // const daysInMonth = currentDate.daysInMonth();
            this.queryParams.dayTime = []
            // 构建日期列表
            this.resetForm("queryForm");
            this.handleQuery();
        }
    }
}
</script>
<style lang='scss' scoped>
#report {
    width: 100%;
    height: 100%;
    padding: 20px;
    position: relative;
    box-sizing: border-box;
}

.title {
    font-size: 16px;
    font-weight: 600;
}

.time-box {
    margin-top: 10px;
    display: flex;
    flex-direction: column;

    >span {
        margin-bottom: 10px;
    }

}

::v-deep .el-date-editor--dates {
    margin-left: 20px;
}
</style>
<style >
.day_form .el-icon-d-arrow-left,
.el-icon-d-arrow-right {
    display: none;
}

.day_form .el-date-picker__header-label:first-of-type {
    display: none;
}
.day_form .el-date-picker__header-label,.date_form .el-date-picker__header-label{
    color: #fff ;
}
.day_form .el-picker-panel__icon-btn{
    color: #fff ;
}

.day_form .el-date-picker__header {
    padding: 6px;
   
}

.date_form .el-month-table td .cell{
    color: #fff ;
}
.date_form .el-month-table td.disabled .cell{
    color: #C0C4CC;
}
</style>